package rabbitmq.topic;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import rabbitmq.utils.MqUtil;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;

public class Producer {
    private final static String TOPIC_MSG="TOPIC_MSG";
    public static void main(String[] args) {
        MqUtil mqUtil=new MqUtil();

        try(Channel chananel = mqUtil.getChananel()){
            chananel.exchangeDeclare(TOPIC_MSG, BuiltinExchangeType.TOPIC);
            HashMap<String,String> msgMap=new HashMap<>();
            msgMap.put("quick.orange.rabbit","被队列 Q1Q2 接收到");
            msgMap.put("lazy.orange.elephant","被队列 Q1Q2 接收到");
            msgMap.put("quick.orange.fox","被队列 Q1 接收到");
            msgMap.put("lazy.brown.fox","被队列 Q2 接收到");
            msgMap.put("lazy.pink.rabbit","虽然满足两个绑定但只被队列 Q2 接收一次");
            msgMap.put("quick.brown.fox","不匹配任何绑定不会被任何队列接收到会被丢弃");
            msgMap.put("quick.orange.male.rabbit","是四个单词不匹配任何绑定会被丢弃");
            msgMap.put("lazy.orange.male.rabbit","是四个单词但匹配 Q2");
            for (Map.Entry<String, String> entry : msgMap.entrySet()) {
                String key = entry.getKey();
                String val = entry.getValue();
                chananel.basicPublish(TOPIC_MSG, key, null, val.getBytes("UTF-8"));
            }
        } catch (TimeoutException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
